import App from './App'
import store from './store/index.js'
// #ifndef VUE3
import Vue from 'vue'
Vue.config.productionTip = false

App.mpType = 'app'
const app = new Vue({
    ...App,
	store
})
app.$mount()
// #endif

// #ifdef VUE3
import { createSSRApp } from 'vue'
export function createApp() {
  const app = createSSRApp(App)
  return {
    app
  }
}
// #endif
Vue.prototype.$checkRegister = function() {
	//let that = this
	uni.request({
		url: getApp().globalData.URL + '/user/checkRegister?openid=' + getApp().globalData.openid,
		method: 'post',
		header: {
			'Content-Type': 'application/x-www-form-urlencoded'
		},
		success: (res) => {
			if (res.data === 1) {
				this.$login()
			} else {}
		}
	})
}

Vue.prototype.$login = function() {
	uni.login({
		success: (res) => {
			if (res.errMsg === 'login:ok') {
				let code = res.code
				uni.request({
					url: getApp().globalData.URL + '/user/login',
					method: 'post',
					data: {
						code: code
					},
					header: {
						'Content-Type': 'application/x-www-form-urlencoded'
					},
					success: (res) => {
						let session = res.data.session_key
						uni.setStorage({
							key: 'session',
							data: res.data.session_key
						})
						this.$getUserInfo()
						uni.getStorage({
							key: 'session',
							success: function(res) {}
						})
					},
				})
			}
		}
	})
}

Vue.prototype.$getUserInfo = function() {
	let session
	uni.getStorage({
		key: 'session',
		success: (res) => {
			session = res.data
			uni.request({
				url: getApp().globalData.URL + '/user/getUserInfo',
				method: 'post',
				header: {
					'Content-Type': 'application/x-www-form-urlencoded',
					'session': session
				},
				success: (res) => {
					getApp().globalData.userInfo = res.data
				}
			})
		}
	})
}